package com.eastfair.certificate.service.impl;

import com.eastfair.auth.entity.OrganizationRole;
import com.eastfair.boot.service.SuperCacheServiceImpl;
import com.eastfair.cache.certificate.AuthCacheKeyBuilder;
import com.eastfair.cache.certificate.CertificateIndexCacheKeyBuilder;
import com.eastfair.certificate.dao.CertificateAuthMapper;
import com.eastfair.certificate.dto.CertificateAuthDTO;
import com.eastfair.certificate.entity.CertificateAuth;
import com.eastfair.certificate.service.CertificateAuthService;
import com.eastfair.boot.service.SuperServiceImpl;

import com.eastfair.certificate.vo.CertificateAuthVO;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.model.CacheKeyBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.util.Collection;

//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
//import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 证件审核表
 * </p>
 *
 * @author ligang
 * @date 2022-07-22
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class CertificateAuthServiceImpl extends SuperCacheServiceImpl<CertificateAuthMapper, CertificateAuth> implements CertificateAuthService {

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(CertificateAuth model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        //雪花ID
        return R.successDef();
    }
    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<CertificateAuth> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    /**
     * 通用查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    public List<CertificateAuth> list(CertificateAuthDTO modelDTO, List<Long> ids) {
        return ((CertificateAuthService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
            magic = @Magic(
                    key = "{" +
                            "{'classify', #args[0]?.classify}," +
                            "{'authSystem', #args[0]?.authSystem}," +
                            "{'id', #args[1]}" +
                            "}"))
    public List<CertificateAuth> listOfCache(CertificateAuthDTO modelDTO, List<Long> ids) {
        //查询
        QueryWrapper<CertificateAuth> queryWrapper = new QueryWrapper<>();
        if (modelDTO != null) {
            queryWrapper.lambda().eq(modelDTO.getClassify() != null, CertificateAuth::getClassify, modelDTO.getClassify());
            queryWrapper.lambda().eq(modelDTO.getAuthSystem() != null, CertificateAuth::getAuthSystem, modelDTO.getAuthSystem());
        }
        if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(CertificateAuth::getId, ids);
        }
        queryWrapper.lambda().eq(CertificateAuth::getIsDeleted, BusinessConstant.DELETE_NO);
        List<CertificateAuth> modelList = list(queryWrapper);
        return modelList;
    }


    /**
     * do转vo
     *
     * @param model model
     * @return modelVo
     */
    @Override
    public CertificateAuthVO doToVo(CertificateAuth model) {
        CertificateAuthVO modelVo = new CertificateAuthVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }

    /**
     * do转vo
     *
     * @param modelList modelList
     * @return list
     */
    @Override
    public List<CertificateAuthVO> doToVos(List<CertificateAuth> modelList) {
        if (modelList == null || modelList.isEmpty()) {
            return null;
        }
        List<CertificateAuthVO> result = new ArrayList<>();
        for (CertificateAuth model : modelList) {
            result.add(doToVo(model));
        }
        return result;
    }

    @Override
    public CacheKeyBuilder getCacheKeyBuilder() {
        return new AuthCacheKeyBuilder();
    }
}
